﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KPWeb.Class;
using KPWeb.Data;





namespace KPWeb.StopkaStrony
{

    public partial class HistoriaLogowan : KPPage
    {
     protected void Page_Load(object sender, EventArgs e)
        {
          

           if (!this.IsPostBack)
           {
               this.WykazFiltrow.SelectedValue = "dataR";
               this.SzukajLogin.Text = String.Format("{0:yyyy-MM-dd}", DateTime.Now);
               this.hFirmaID.Value = Current.FirmaID;
      //         this.ListView1.DataBind(); ####
           }
    
        }

     protected void btnSzukaj_Click(object sender, EventArgs e)
     {
         //  string strTemp = this.SzukajLogin.Text;
     }

        static public IEnumerable<vKP_HistoriaLogowania> PobierzHistorieLogowan(string filtr, string szukaj, int FirmaID)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                 

                    if (filtr == "login")
                        if (szukaj == null)
                            return (db.vKP_HistoriaLogowania.Where(a => a.FirmaID == FirmaID).OrderByDescending(a=>a.TimeStart).ToArray());
                         else
                            return (db.vKP_HistoriaLogowania.Where(a => a.Login.IndexOf(szukaj) >= 0 && a.FirmaID == FirmaID).OrderByDescending(a => a.TimeStart).ToArray());
                    else
                        if (szukaj != null)
                            switch (filtr)
                            {
                                case "uprawnienia":
                                    return (db.vKP_HistoriaLogowania.Where(a => a.Uprawnienia.Trim().ToUpper() == szukaj.Trim().ToUpper()
                                          && a.FirmaID == FirmaID).OrderByDescending(a => a.TimeStart).ToArray());

                                case "jednostka":
                                    return (db.vKP_HistoriaLogowania.Where(a => a.JednostkaKod.Trim().ToUpper() == szukaj.Trim().ToUpper()
                                          && a.FirmaID == FirmaID).OrderByDescending(a => a.TimeStart).ToArray());
                                case "dataR":
                                    DateTime dt1 = DateTime.Parse(szukaj).Date;
                                    DateTime dt2 = DateTime.Parse(szukaj).AddDays(1).Date;
                                    return (db.vKP_HistoriaLogowania.Where(a => a.TimeStart >= dt1 && a.TimeStart < dt2
                                          && a.FirmaID == FirmaID).OrderByDescending(a => a.TimeStart).ToArray());
                                case "dataRW":
                                    DateTime dt = DateTime.Parse(szukaj).Date;
                                    return (db.vKP_HistoriaLogowania.Where(a => a.TimeStart>=  dt
                                          && a.FirmaID == FirmaID).OrderByDescending(a => a.TimeStart).ToArray());
                                  
                                default:
                                    return (null);
                            }
                        else

                            return (null);

                }
            }
            catch
            {
                return (null);
            }
        }


    }
}
